<?php

class Yingfu extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model("base_model");
        $this->load->model("order_model");
        $this->load->model("custom_model");
        $this->load->model("car_model");
        $this->load->model("custom_address_model");
        $this->load->model("province_model");
        $this->load->model("city_model");
        $this->load->model("district_model");
        $this->load->model("admin_user_model");
        $this->load->model("driver_model");
        $this->load->model("order_fee_model");
        $this->load->model("order_fee_pay_model");
        $this->load->model("shouzhi_bank_model");

        $this->load->service("admin_manage_service");
        $this->check = $this->admin_manage_service;
        $this->_adminInfo = $this->check->checkAdminLogin();
        #

        $this->projectSet = [
            "url" => [
                "view" => MANAGE_URL . "&c=yingfu&m=view",
                "list" => MANAGE_URL . "&c=yingfu&m=lists",
                "batchPay" => MANAGE_URL . "&c=orderFee&m=view",
                "doBatchPay" => MANAGE_URL . "&c=yingfu&m=doBatchPay",
                "hexiao" => MANAGE_URL . "&c=yingfu&m=hexiao",
                "cancelHexiao" => MANAGE_URL . "&c=yingfu&m=cancelHexiao",
                "doSelectIds" => MANAGE_URL . "&c=yingfu&m=doSelectIds",
            ],
            "file" => [
                "view" => "admin/yingfu",
                "js" => "admin/js/yingfu",
                "cache_ids_name" => "cache_yingfu_ids",
                "power_name" => "yingfu"
            ],
        ];
    }

    /*
     * 列表
     */

    function lists() {

        try {

            #权限
            $this->check->checkPower(array("power" => $this->projectSet["file"]["power_name"] . "_view"));

            $data["navName"] = "应付管理";

            $page = $this->input->getInt("page", TRUE);
            if ($page <= 0) {
                $page = 1;
            }
            $pageCount = $this->input->getInt("limit", TRUE);
            if ($pageCount <= 0) {
                $pageCount = 50;
            }
            if ($pageCount >= 200) {
                $pageCount = 200;
            }
            $data["page"] = $page;
            $data["pageCount"] = $pageCount;

            $first = $this->input->getInt("first", true);
            if ($first == 1) {
                $this->session->unset_userdata([$this->projectSet["file"]["cache_ids_name"]]);
            }

            $searchParam = $this->_searchParam();
            $where = $searchParam["where"];
            $pageStrArr = $searchParam["pageStrArr"];
            foreach ($searchParam["data"] as $k => $v) {
                ${$k} = $v;
                $data[$k] = $v;
            }

            $type = $this->order_model->type();
            $data["type"] = $type;

            $secondType = $this->order_model->secondType();
            $data["secondType"] = $secondType;

            $status = $this->order_model->status();
            $data["status"] = $status;

            $hexiaoStatus = $this->order_model->hexiaoStatus();
            $data["hexiaoStatus"] = $hexiaoStatus;

            $carType = $this->car_model->carType();

            $data["kehu"] = $this->custom_model->getData("where type_id=2 and status_id=1", 1, 0);
            //$data["diaodu"] = $this->base_model->getData("select au.* from " . $this->admin_user_model->tableName . " au left join " . $this->admin_role_model->tableName . " ar on au.role_id=ar.id where ar.type_id=1 and au.status_id=1", 1, 0, " order by au.id desc");
            $data["car"] = $this->car_model->getData("where status_id=1", 1, 0);

            $province = $this->province_model->getDataSetKey();
            //$city = $this->city_model->getDataSetKey();
            //$district = $this->district_model->getDataSetKey();

            $sql = " from " . $this->order_model->tableName . " o";
            $sql .= " left join " . $this->car_model->tableName . " c on o.car_id=c.id";
            $sql .= " left join " . $this->custom_model->tableName . " ct on o.kehu_custom_id=ct.id";
            $sql .= " left join " . $this->admin_user_model->tableName . " diaodu on o.diaodu_user_id=diaodu.id";
            //$sql .= " left join " . $this->admin_user_model->tableName . " cwjl on o.cwjl_user_id=cwjl.id";
            //$sql .= " left join " . $this->driver_model->tableName . " zhujia on o.zhujia_driver_id=zhujia.id";
            //$sql .= " left join " . $this->driver_model->tableName . " fujia on o.fujia_driver_id=fujia.id";

            $sql .= " left join (select order_id,SUM(total_fee) AS sum_total_fee,SUM(CASE WHEN fee_type = 2 THEN end_mileage-start_mileage ELSE 0 END) AS total_mileage,
                            SUM(CASE WHEN fee_type = 2 THEN num ELSE 0 END) AS total_jiayou from " . $this->order_fee_model->tableName . " where type_id=2 group by order_id) of on of.order_id=o.id";
            $sql .= " left join (select order_id,sum(total_fee) as sum_total_fee from " . $this->order_fee_pay_model->tableName . " where type_id=2 group by order_id) ofp on ofp.order_id=o.id";

            $listNum = $this->base_model->getDataCount("select o.id" . $sql . " " . $where . " group by o.id");

            $is_ajax = $this->input->is_ajax_request();
            if ($is_ajax) {

                $select = "select o.*,
                    sum(completed_at-setting_off_at) as total_haoshi,
                    sum(delivering_at-arrived_at) as total_zhuangzaishi,
                    sum(completed_at-at_delivery_at) as total_xiehuoshi";
                $select .= ",ct.cmp_bf as ct_cmp_bf,diaodu.truename as diaodu_truename,c.car_number as c_car_number,c.car_type as c_car_type";
                $select .= ",of.sum_total_fee AS total_yingfu
                            ,ofp.sum_total_fee AS total_yifu
                            ,of.total_mileage AS total_mileage
                            ,of.total_jiayou AS total_jiayou";

                $dataList = $this->base_model->getData($select . $sql . " " . $where, $page, $pageCount, "group by o.id order by o.id desc");

                foreach ($dataList as $k => $v) {
                    
                    $dataList[$k]["youhao"] = $v["total_mileage"] > 0 ? round(($v["total_jiayou"] / $v["total_mileage"]) * 100, 2) : 0;
     
                    $dataList[$k]["status_name"] = $status[$v["status_id"]]["name"];
                    $dataList[$k]["order_date_alias"] = date("Y-m-d", $v["order_date"]);

                    $org_province = !empty($province[$v["org_province_id"]]) ? $province[$v["org_province_id"]]["name"] : "";
                    //$org_city = !empty($city[$v["org_city_id"]]) ? $city[$v["org_city_id"]]["name"] : "";
                    //$org_district = !empty($district[$v["org_district_id"]]) ? $province[$v["org_district_id"]]["name"] : "";
                    //$dataList[$k]["org_tag"] = $org_province . "/" . $org_city . "/" . $v["org_address"];
                    $dataList[$k]["org_tag"] = $org_province . "/" . $v["org_address"];

                    $dest_province = !empty($province[$v["dest_province_id"]]) ? $province[$v["dest_province_id"]]["name"] : "";
                    //$dest_city = !empty($city[$v["dest_city_id"]]) ? $city[$v["dest_city_id"]]["name"] : "";
                    //$dest_district = !empty($district[$v["dest_district_id"]]) ? $district[$v["dest_district_id"]]["name"] : "";
                    //$dataList[$k]["dest_tag"] = $dest_province . "/" . $dest_city . "/" . $v["dest_address"];
                    $dataList[$k]["dest_tag"] = $dest_province . "/" . $v["dest_address"];

                    $dataList[$k]["hexiao_status_tag"] = $hexiaoStatus[$v["hexiao_status"]]["name"];
                }
                $data["dataList"] = $dataList;

                $result = [
                    "code" => 0,
                    "count" => $listNum,
                    "data" => $dataList,
                    "msg" => ""
                ];
                echo jsonEncode($result);
                exit;
            }

            #进行分页
            $pageStr = "";
            if (!empty($pageStrArr)) {
                foreach ($pageStrArr as $k => $v) {
                    $pageStr .= "&" . $k . "=" . $v;
                }
            }
            $data['base_url'] = $this->projectSet["url"]["list"] . $pageStr;

            $data["listNum"] = $listNum;
            $data["pageStr"] = $pageStr;

            $this->layout->view($this->projectSet["file"]["view"] . '/list', $data);
        } catch (Exception $e) {
            show_error($msg = $e->getMessage(), $e->getCode() ? $e->getCode() : 200, "系统提示");
        }
    }

    public function _searchParam() {

        $pageStrArr = array();
        $where = "where 1";

        $status_id = $this->input->getPostStr("status_id", TRUE);
        if ($status_id != "") {
            $pageStrArr["status_id"] = $status_id;
            $where .= " AND o.status_id = '" . $status_id . "'";
        }
        $data["status_id"] = $status_id;

        $type_id = $this->input->getPostInt("type_id", TRUE);
        if ($type_id > 0) {
            $pageStrArr["type_id"] = $type_id;
            $where .= " AND o.type_id = " . $type_id;
        }
        $data["type_id"] = $type_id;

        $second_type_id = $this->input->getPostInt("second_type_id", TRUE);
        if ($second_type_id > 0) {
            $pageStrArr["second_type_id"] = $second_type_id;
            $where .= " AND o.second_type_id = " . $second_type_id;
        }
        $data["second_type_id"] = $second_type_id;

        $car_id = $this->input->getPostInt("car_id", TRUE);
        if ($car_id > 0) {
            $pageStrArr["car_id"] = $car_id;
            $where .= " AND o.car_id = " . $car_id;
        }
        $data["car_id"] = $car_id;

        $order_date_range = $this->input->getPostStr("order_date_range", TRUE);
        if ($order_date_range === null) {
            $order_date_range = date("Y-m-d", strtotime("-29 days")) . " - " . date("Y-m-d");
        }
        if ($order_date_range != "") {
            $order_date_range_arr = explode(" - ", $order_date_range);
            $order_date_start = strtotime($order_date_range_arr[0]);
            $where .= " AND o.order_date >=" . $order_date_start;
            $order_date_end = strtotime($order_date_range_arr[1]) + 3600 * 24;
            $where .= " AND o.order_date <" . $order_date_end;
        }
        $pageStrArr["order_date_range"] = $order_date_range;
        $data["order_date_range"] = $order_date_range;

        $order_no = $this->input->getPostStr("order_no", TRUE);
        if ($order_no != "") {
            $pageStrArr["order_no"] = $order_no;
            $where .= " AND o.order_no like '%" . $order_no . "%'";
        }
        $data["order_no"] = $order_no;

        $kehu_custom_id = $this->input->getPostInt("kehu_custom_id", TRUE);
        if ($kehu_custom_id > 0) {
            $pageStrArr["kehu_custom_id"] = $kehu_custom_id;
            $where .= " AND o.kehu_custom_id = " . $kehu_custom_id;
        }
        $data["kehu_custom_id"] = $kehu_custom_id;

        $hexiao_status = $this->input->getPostStr("hexiao_status", TRUE);
        if ($hexiao_status !== null && $hexiao_status !== "") {
            $pageStrArr["hexiao_status"] = $hexiao_status;
            $where .= " AND o.hexiao_status = " . $hexiao_status;
        }
        $data["hexiao_status"] = $hexiao_status;

        return [
            "data" => $data,
            "pageStrArr" => $pageStrArr,
            "where" => $where,
        ];
    }

    public function doSelectIds() {

        #权限
        //$this->check->checkPower(array("power" => "idcard_edit", "ajax" => 1));

        $flag = 0;
        $msg = "操作失败";
        $id_arr = $this->input->postStr("ids", TRUE, false);

        try {
            $sessionData = [
                $this->projectSet["file"]["cache_ids_name"] => $id_arr,
            ];
            $this->session->set_userdata($sessionData);

            $flag = 1;
            $msg = "操作成功";
            //事务结束
        } catch (Exception $e) {
            $msg = $e->getMessage();
        }
        $outArr = array("flag" => $flag, "msg" => $msg);
        echo jsonEncode($outArr);
    }

    /*
     * 
     */

    function view() {

        #权限

        $data["navName"] = "查看订单利润";

        try {

            $this->check->checkPower(array("power" => $this->projectSet["file"]["power_name"] . "_view",));

            $order_id = $this->input->getInt("order_id", TRUE);
            $order = $this->order_model->getDataById($order_id);

            if (empty($order)) {
                throw new Exception("不存在的订单");
            }

            $is_ajax = $this->input->is_ajax_request();
            if ($is_ajax) {

                $type = $this->order_model->type();
                $data["type"] = $type;

                $secondType = $this->order_model->secondType();
                $data["secondType"] = $secondType;

                $status = $this->order_model->status();
                $data["status"] = $status;

                $carType = $this->car_model->carType();

                $province = $this->province_model->getDataSetKey();
                $city = $this->city_model->getDataSetKey();
                $district = $this->district_model->getDataSetKey();

                $where = "where o.same_order_id=" . $order_id;

                $sql = " from " . $this->order_model->tableName . " o";
                $sql .= " left join " . $this->car_model->tableName . " c on o.car_id=c.id";
                $sql .= " left join " . $this->custom_model->tableName . " ct on o.kehu_custom_id=ct.id";
                $sql .= " left join " . $this->admin_user_model->tableName . " diaodu on o.diaodu_user_id=diaodu.id";
                $sql .= " left join " . $this->admin_user_model->tableName . " cwjl on o.cwjl_user_id=cwjl.id";
                $sql .= " left join " . $this->driver_model->tableName . " zhujia on o.zhujia_driver_id=zhujia.id";
                $sql .= " left join " . $this->driver_model->tableName . " fujia on o.fujia_driver_id=fujia.id";

                $sql .= " left join " . $this->order_fee_model->tableName . " of on of.same_order_id=o.id";

                $select = "select o.*,
                    sum(completed_at-setting_off_at) as total_haoshi,
                    sum(delivering_at-arrived_at) as total_zhuangzaishi,
                    sum(completed_at-at_delivery_at) as total_xiehuoshi";
                $select .= ",ct.cmp_bf as ct_cmp_bf,diaodu.truename as diaodu_truename,cwjl.truename as cwjl_truename,c.car_number as c_car_number,c.car_type as c_car_type,zhujia.driver_name as zhujia_driver_name,zhujia.tel as zhujia_tel,fujia.driver_name as fujia_driver_name,fujia.tel as fujia_tel";
                $select .= ",SUM(CASE WHEN of.type_id = 1 THEN of.total_fee ELSE 0 END) AS total_yingshou,
                            SUM(CASE WHEN of.type_id = 2 THEN of.total_fee ELSE 0 END) AS total_yingfu,
                            SUM(CASE WHEN of.type_id = 1 AND of.pay_status = 1 THEN of.total_fee ELSE 0 END) AS total_yishou,
                            SUM(CASE WHEN of.type_id = 2 AND of.pay_status = 1 THEN of.total_fee ELSE 0 END) AS total_yifu,
                            SUM(CASE WHEN of.fee_type = 2 THEN of.end_mileage-of.start_mileage ELSE 0 END) AS total_mileage,
                            SUM(CASE WHEN of.fee_type = 2 THEN of.num ELSE 0 END) AS total_jiayou";

                $dataList = $this->base_model->getData($select . $sql . " " . $where, 1, 0, "group by o.id order by o.id desc");

                foreach ($dataList as $k => $v) {

                    $total_haoshi = round($v["total_haoshi"] / 3600 / 24, 2);
                    $dataList[$k]["total_haoshi"] = $total_haoshi;

                    $dataList[$k]["meiri_mileage"] = $total_haoshi > 0 ? round($v["total_mileage"] / $total_haoshi, 2) : 0;
                    $dataList[$k]["youhao"] = $v["total_mileage"] > 0 ? round(($v["total_jiayou"] / $v["total_mileage"]) * 100, 2) : 0;

                    $total_lirun = $v["total_yishou"] - $v["total_yifu"];
                    $dataList[$k]["total_lirun"] = $total_lirun;
                    $dataList[$k]["meiri_lirun"] = $total_haoshi > 0 ? round($total_lirun / $total_haoshi, 2) : 0;

                    $total_zhuangzaishi = round($v["total_zhuangzaishi"] / 3600 / 24, 2);
                    $dataList[$k]["total_zhuangzaishi"] = $total_zhuangzaishi;
                    $total_xiehuoshi = round($v["total_xiehuoshi"] / 3600 / 24, 2);
                    $dataList[$k]["total_xiehuoshi"] = $total_xiehuoshi;
                    $dataList[$k]["total_xingshi"] = $total_haoshi - $total_zhuangzaishi - $total_xiehuoshi;

                    $dataList[$k]["created_at_alias"] = date("Y-m-d H:i:s", $v["created_at"]);
                    $dataList[$k]["updated_at_alias"] = date("Y-m-d H:i:s", $v["updated_at"]);
                    $dataList[$k]["type_name"] = $type[$v["type_id"]]["name"];
                    $dataList[$k]["type_name"] .= $v["second_type_id"] > 0 ? "-" . $secondType[$v["second_type_id"]]["name"] : "";
                    $dataList[$k]["status_name"] = $status[$v["status_id"]]["name"];
                    $dataList[$k]["order_date_alias"] = date("Y-m-d", $v["order_date"]);
                    $org_province = !empty($province[$v["org_province_id"]]) ? $province[$v["org_province_id"]]["name"] : "";
                    $org_city = !empty($city[$v["org_city_id"]]) ? $city[$v["org_city_id"]]["name"] : "";
                    $org_district = !empty($district[$v["org_district_id"]]) ? $province[$v["org_district_id"]]["name"] : "";
                    $dataList[$k]["org_tag"] = $org_province . "/" . $org_city . "/" . $v["org_address"];

                    $dest_province = !empty($province[$v["dest_province_id"]]) ? $province[$v["dest_province_id"]]["name"] : "";
                    $dest_city = !empty($city[$v["dest_city_id"]]) ? $city[$v["dest_city_id"]]["name"] : "";
                    $dest_district = !empty($district[$v["dest_district_id"]]) ? $district[$v["dest_district_id"]]["name"] : "";
                    $dataList[$k]["dest_tag"] = $dest_province . "/" . $dest_city . "/" . $v["dest_address"];
                    $dataList[$k]["c_car_type_name"] = !empty($carType[$v["c_car_type"]]) ? $carType[$v["c_car_type"]]["name"] : "";
                    $dataList[$k]["xianghao_tag"] = $v["xianghao"] . ($v["xianghao2"] ? "," . $v["xianghao2"] : "");
                    $dataList[$k]["setting_off_at_alias"] = $v["setting_off_at"] > 0 ? date("m/d H:i", $v["setting_off_at"]) : "";
                    $dataList[$k]["arrived_at_alias"] = $v["arrived_at"] > 0 ? date("m/d H:i", $v["arrived_at"]) : "";
                    $dataList[$k]["loading_at_alias"] = $v["loading_at"] > 0 ? date("m/d H:i", $v["loading_at"]) : "";
                    $dataList[$k]["delivering_at_alias"] = $v["delivering_at"] > 0 ? date("m/d H:i", $v["delivering_at"]) : "";
                    $dataList[$k]["at_delivery_at_alias"] = $v["at_delivery_at"] > 0 ? date("m/d H:i", $v["at_delivery_at"]) : "";
                    $dataList[$k]["off_loading_at_alias"] = $v["off_loading_at"] > 0 ? date("m/d H:i", $v["off_loading_at"]) : "";
                    $dataList[$k]["completed_at_alias"] = $v["completed_at"] > 0 ? date("m/d H:i", $v["completed_at"]) : "";
                }

                $result = [
                    "code" => 0,
                    "count" => count($dataList),
                    "data" => $dataList,
                    "msg" => ""
                ];
                echo jsonEncode($result);
                exit;
            }
            $data["base_url"] = $this->projectSet["url"]["view"] . "&order_id=" . $order_id;

            $this->layout->view($this->projectSet["file"]["view"] . '/view', $data);
        } catch (Exception $e) {
            $msg = $e->getMessage();
            show_error($msg, 200, "系统提示");
        }
    }

    /*
     * 
     */

    function batchPay() {

        try {
            $this->check->checkPower(array("power" => $this->projectSet["file"]["power_name"] . "_pay"));

            $where = "";
            $is_sql = $this->input->getInt("is_sql", true);
            if ($is_sql == 1) {
                $searchParam = $this->_searchParam();
                $where = $searchParam["where"];
            } else {
                $cache_id_arr = $this->session->userdata($this->projectSet["file"]["cache_ids_name"]);
                if (empty($cache_id_arr)) {
                    throw new Exception("没有选择数据");
                }
                $ids = join(",", $cache_id_arr);
                $where = "where id in (" . $ids . ")";
            }
            $dataS = $this->order_model->getData($where, 1, 0, "");
            if (empty($dataS)) {
                throw new Exception("不存在数据");
            }
            if (count($dataS) > 1) {
                throw new Exception("一次只能选择一个订单操作");
            }
            $oldData = $dataS[0];
            if ($oldData["hexiao_status"] == 1) {
                throw new Exception("该订单已经核销，不可操作");
            }
            $order_id = $oldData["id"];

            $yingfu_fee = 0;
            $orderFee = $this->order_fee_model->getData("where type_id=2 and order_id=" . $order_id, 1, 0, "", "sum(total_fee) as yingfu_fee");
            if (!empty($orderFee[0]["yingfu_fee"])) {
                $yingfu_fee = $orderFee[0]["yingfu_fee"];
            }
            $data["yingfu_fee"] = $yingfu_fee;

            $yifu_fee = 0;
            $orderFeePay = $this->order_fee_pay_model->getData("where type_id=2 and order_id=" . $order_id, 1, 0, "", "sum(total_fee) as yifu_fee");
            if (!empty($orderFeePay[0]["yifu_fee"])) {
                $yifu_fee = $orderFeePay[0]["yifu_fee"];
            }
            $data["yifu_fee"] = $yifu_fee;

            $data["shouzhiBank"] = $this->shouzhi_bank_model->getData("where status_id=1", 1, 0);

            $data["doAddUrl"] = $this->projectSet["url"]["doBatchPay"];

            $this->layout->view($this->projectSet["file"]["view"] . '/batchPay', $data);
        } catch (Exception $e) {
            show_error($msg = $e->getMessage(), $e->getCode() ? $e->getCode() : 200, "系统提示");
        }
    }

    function doBatchPay() {

        #权限

        $flag = 0;
        $msg = "操作失败";

        try {

            $this->check->checkPower(array("power" => $this->projectSet["file"]["power_name"] . "_pay"));

            $needField = $this->order_fee_pay_model->needField();
            $fieldValue = $this->common->checkField($needField);

            $fieldValue["pay_time"] = strtotime($fieldValue["pay_time"]);

            $where = "";
            $is_sql = $this->input->getInt("is_sql", true);
            if ($is_sql == 1) {
                $searchParam = $this->_searchParam();
                $where = $searchParam["where"];
            } else {
                $cache_id_arr = $this->session->userdata($this->projectSet["file"]["cache_ids_name"]);
                if (empty($cache_id_arr)) {
                    throw new Exception("没有选择数据");
                }
                $ids = join(",", $cache_id_arr);
                $where = "where id in (" . $ids . ")";
            }
            $dataS = $this->order_model->getData($where, 1, 0, "");
            if (empty($dataS)) {
                throw new Exception("不存在数据");
            }
            if (count($dataS) > 1) {
                throw new Exception("一次只能选择一个订单操作");
            }
            $oldData = $dataS[0];
            if ($oldData["hexiao_status"] == 1) {
                throw new Exception("该订单已经核销，不可操作");
            }
            $order_id = $oldData["id"];

            $yingfu_fee = 0;
            $orderFee = $this->order_fee_model->getData("where type_id=2 and order_id=" . $order_id, 1, 0, "", "sum(total_fee) as yingfu_fee");
            if (!empty($orderFee[0]["yingfu_fee"])) {
                $yingfu_fee = $orderFee[0]["yingfu_fee"];
            }
            $data["yingfu_fee"] = $yingfu_fee;

            $yifu_fee = 0;
            $orderFeePay = $this->order_fee_pay_model->getData("where type_id=2 and order_id=" . $order_id, 1, 0, "", "sum(total_fee) as yifu_fee");
            if (!empty($orderFeePay[0]["yifu_fee"])) {
                $yifu_fee = $orderFeePay[0]["yifu_fee"];
            }
            $data["yifu_fee"] = $yifu_fee;

            if ($fieldValue["total_fee"] > ($yingfu_fee - $yifu_fee)) {
                throw new Exception("付款金额不能大于(应付-已付)金额");
            }
            $this->db->trans_start();

            $uData = array_merge($fieldValue, [
                "order_id" => $order_id,
                "type_id" => 2,
                "created_user_id" => $this->_adminInfo["id"],
                "created_at" => time(),
                "updated_at" => time(),
            ]);
            $insert_id = $this->order_fee_pay_model->addData($uData);

            $flag = 1;
            $msg = "操作成功";

            $this->db->trans_complete();
        } catch (Exception $e) {
            $msg = $e->getMessage();
        }

        $outArr = array("flag" => $flag, "msg" => $msg);
        echo jsonEncode($outArr);
    }

    /*
     * 
     */

    public function hexiao() {

        #权限

        $flag = 0;
        $msg = "操作失败";

        try {

            $this->check->checkPower(array("power" => $this->projectSet["file"]["power_name"] . "_hexiao"));

            $where = "";
            $is_sql = $this->input->getInt("is_sql", true);
            if ($is_sql == 1) {
                $searchParam = $this->_searchParam();
                $where = $searchParam["where"];
            } else {
                $cache_id_arr = $this->session->userdata($this->projectSet["file"]["cache_ids_name"]);
                if (empty($cache_id_arr)) {
                    throw new Exception("没有选择数据");
                }
                $ids = join(",", $cache_id_arr);
                $where = "where id in (" . $ids . ")";
            }
            $dataS = $this->order_model->getData($where, 1, 0, "");
            if (empty($dataS)) {
                throw new Exception("不存在数据");
            }

            $id_arr = array_column($dataS, "id");
            $ids = join(",", $id_arr);

            $this->db->trans_start();

            $uData = [
                "hexiao_status" => 1,
                "updated_at" => time(),
            ];
            $insert_id = $this->order_model->updateData([
                "fieldArr" => $uData,
                "whereArr" => "id in(" . $ids . ")",
            ]);

            $flag = 1;
            $msg = "操作成功";

            $this->db->trans_complete();
        } catch (Exception $e) {
            $msg = $e->getMessage();
        }

        $outArr = array("flag" => $flag, "msg" => $msg);
        echo jsonEncode($outArr);
    }

    /*
     * 
     */

    public function cancelHexiao() {

        #权限

        $flag = 0;
        $msg = "操作失败";

        try {

            $this->check->checkPower(array("power" => $this->projectSet["file"]["power_name"] . "_cancelHexiao"));

            $where = "";
            $is_sql = $this->input->getInt("is_sql", true);
            if ($is_sql == 1) {
                $searchParam = $this->_searchParam();
                $where = $searchParam["where"];
            } else {
                $cache_id_arr = $this->session->userdata($this->projectSet["file"]["cache_ids_name"]);
                if (empty($cache_id_arr)) {
                    throw new Exception("没有选择数据");
                }
                $ids = join(",", $cache_id_arr);
                $where = "where id in (" . $ids . ")";
            }
            $dataS = $this->order_model->getData($where, 1, 0, "");
            if (empty($dataS)) {
                throw new Exception("不存在数据");
            }

            $id_arr = array_column($dataS, "id");
            $ids = join(",", $id_arr);

            $this->db->trans_start();

            $uData = [
                "hexiao_status" => 0,
                "updated_at" => time(),
            ];
            $insert_id = $this->order_model->updateData([
                "fieldArr" => $uData,
                "whereArr" => "id in(" . $ids . ")",
            ]);

            $flag = 1;
            $msg = "操作成功";

            $this->db->trans_complete();
        } catch (Exception $e) {
            $msg = $e->getMessage();
        }

        $outArr = array("flag" => $flag, "msg" => $msg);
        echo jsonEncode($outArr);
    }

}
